<?php
namespace Controllers;
use Libraries;
class Proyek extends Libraries\PController {
    private $id, $nama, $lokasi, $status, $tgl_mulai, $tgl_selesai;
    public function __construct() {
        parent::__construct();
        $this->db = new Libraries\Mysql;
    }
    public function index() {
        if (Libraries\u::isAjaxRequest()) {
            $proyek = $this->db->results("SELECT * FROM psn_proyek");
            $this->outputJSON(array('success' => 'true', 'results' => $proyek));
        } else {
            $this->redirect('home');
        }
    }
    public function create() {
        if (Libraries\u::isAjaxRequest()) {
            $this->get_submit();
            $this->db->results(Libraries\CDb::insert_proyek($this->nama, $this->lokasi,
                            $this->tgl_mulai, $this->tgl_selesai, $this->status));
            $id = $this->db->insertId();
            $msg = "Data berhasil di simpan dengan id $id";
            $this->outputJSON(array('success' => true, 'msg' => $msg));
        } else {
            $this->redirect('home');
        }
    }
    public function update() {
        if (Libraries\u::isAjaxRequest()) {
            $this->get_submit();
            $msg = "Data dengan id $this->id berhasil di simpan.";
            $this->db->results(Libraries\CDb::update_proyek($this->id, $this->nama, $this->lokasi,
                            $this->tgl_mulai, $this->tgl_selesai, $this->status));
            $this->outputJSON(array('success' => true, 'msg' => $msg));
        } else {
            $this->redirect('home');
        }
    }
    private function get_submit() {
        foreach ($_POST as $key => $val) {
            $_POST[$key] = $this->request->post($key,
                    FILTER_SANITIZE_MAGIC_QUOTES);
            $_POST[$key] = $this->db->escape($_POST[$key]);
            $this->$key = $val;
        }
    }
}
